package com.zhongketech.sunflower.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Looper;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.text.Layout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhongketech.sunflower.Constant;
import com.zhongketech.sunflower.MyApplication;
import com.zhongketech.sunflower.MyToast;
import com.zhongketech.sunflower.R;
import com.zhongketech.sunflower.activity.me.PersonalSettingActivity;
import com.zhongketech.sunflower.activity.me.SettingActivity;
import com.zhongketech.sunflower.adapter.FcsContact;
import com.zhongketech.sunflower.app.AppManager;
import com.zhongketech.sunflower.control.PhoneManager;
import com.zhongketech.sunflower.droidwall.DroidWallHandle;
import com.zhongketech.sunflower.sv.SvManager;
import com.zhongketech.sunflower.transport.Action;
import com.zhongketech.sunflower.transport.LogUtil;
import com.zhongketech.sunflower.transport.im.ZKSocket;
import com.zhongketech.sunflower.transport.model.MyRequest;
import com.zhongketech.sunflower.transport.model.Parents;
import com.zhongketech.sunflower.transport.model.User;
import com.zhongketech.sunflower.utils.AsyncBitmapLoader;
import com.zhongketech.sunflower.utils.AsyncBitmapLoader.ImageCallBack;
import com.zhongketech.sunflower.utils.AsyncImageLoader;
import com.zhongketech.sunflower.utils.PreferenceUtils;
/**
 * 我的界面
 * @author Administrator
 *
 */
public class SettingsFragment extends Fragment implements OnClickListener{

	private static final String TAG = "SettingsFragment";

	private static final int layoutIdList[] = {
		R.id.fcs_person_layout,
		R.id.fcs_control_layout,
		R.id.fcs_time_layout,
		R.id.fcs_special_number_layout,
		R.id.fcs_qr_layout,
		R.id.fcs_patriarch_layout,
		R.id.fcs_binding_layout,
		R.id.fcs_set_layout};

	private Context context ;

	//control icon
	private ImageView fcs_control_phone;
	private ImageView fcs_control_sms;
	private ImageView fcs_control_lock;
	private ImageView fcs_control_net;
	private ImageView fcs_control_eyes;
	private ImageView fcs_control_app;

	//person
	private ImageView fcs_default_head;
	private TextView fcs_person_name;
	private TextView fcs_person_id;
	private TextView fcs_person_info;


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


		return inflater.inflate(R.layout.fragment_conversation_settings, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		context = getActivity();
		initView();
	}

	private void initView()
	{
		for(int i = 0;i < layoutIdList.length;i++)
		{
			getView().findViewById(layoutIdList[i]).setOnClickListener(this);
		}

		fcs_control_phone = (ImageView) getView().findViewById(R.id.fcs_control_phone);
		fcs_control_sms = (ImageView) getView().findViewById(R.id.fcs_control_sms);
		fcs_control_lock = (ImageView) getView().findViewById(R.id.fcs_control_lock);
		fcs_control_net = (ImageView) getView().findViewById(R.id.fcs_control_net);
		fcs_control_eyes = (ImageView) getView().findViewById(R.id.fcs_control_eyes);
		fcs_control_app = (ImageView) getView().findViewById(R.id.fcs_control_app);
		//person
		fcs_default_head = (ImageView) getView().findViewById(R.id.fcs_default_head);
		fcs_person_name = (TextView) getView().findViewById(R.id.fcs_person_name);
		fcs_person_id = (TextView) getView().findViewById(R.id.fcs_person_id);
		fcs_person_info = (TextView) getView().findViewById(R.id.fcs_person_info);

	}


	private void setHeadIcon()
	{
		String url = MyApplication.getInstance().getAvatar_url();
		if(null != url && url.startsWith("http"))
		{
			LogUtil.i("ZK","setHeadIcon:"+url);
			AsyncBitmapLoader.getInstance().loadBitmap(fcs_default_head, url, new ImageCallBack() {

				@Override
				public void imageLoad(ImageView imageView, Bitmap bitmap) {
					// TODO Auto-generated method stub
					LogUtil.i("ZK","setHeadIcon get  bitmap");
					if(null != bitmap)
					{
						fcs_default_head.setImageBitmap(bitmap);	
					}
					
				}
			});
		}

	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		SvManager.getInstance().check();
		LogUtil.i("ZK","SettingsFragment onResume");
		initData();
		setHeadIcon();
	}

	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		SvManager.getInstance().check();
		LogUtil.i("ZK","SettingsFragment onHiddenChanged");
		initData();
	}

	private void initData()
	{
		if(SvManager.getInstance().isPhoneEnable())
		{
			fcs_control_phone.setImageResource(R.drawable.fcs_control_phone_enable);
		}
		else
		{
			fcs_control_phone.setImageResource(R.drawable.fcs_control_phone_disable);
		}

		if(SvManager.getInstance().isSmsEnable())
		{
			fcs_control_sms.setImageResource(R.drawable.fcs_control_sms_enable);
		}
		else
		{
			fcs_control_sms.setImageResource(R.drawable.fcs_control_sms_disable);
		}

		if(SvManager.getInstance().isLock())
		{
			fcs_control_lock.setImageResource(R.drawable.fcs_control_lock_enable);
		}
		else
		{
			fcs_control_lock.setImageResource(R.drawable.fcs_control_lock_disable);
		}

		if(SvManager.getInstance().isWebEnable())
		{
			fcs_control_net.setImageResource(R.drawable.fcs_control_net_enable);
		}
		else
		{
			fcs_control_net.setImageResource(R.drawable.fcs_control_net_disable);
		}

		if(Constant.APP_DISABLE_ENABLE.equals(SvManager.getInstance().appEnable()) || Constant.APP_LIST.equals(SvManager.getInstance().appEnable()))
		{
			fcs_control_app.setImageResource(R.drawable.fcs_control_app_enable);
		}
		else
		{
			fcs_control_app.setImageResource(R.drawable.fcs_control_app_disable);
		}

		if(SvManager.getInstance().eyesEnable())
		{
			fcs_control_eyes.setImageResource(R.drawable.fcs_control_eyes_enable);  
		}
		else
		{
			fcs_control_eyes.setImageResource(R.drawable.fcs_control_eyes_disable);
		}

		fcs_person_name.setText(MyApplication.getInstance().getName());
		fcs_person_id.setText("ID:"+MyApplication.getInstance().getVisibleId());
	}


	@Override
	public void onClick(View v) {

		//Log.d(TAG,"v .................:"+v);

		switch (v.getId()) {
		case R.id.fcs_person_layout: 
			startActivity(new Intent(context.getApplicationContext(),PersonalSettingActivity.class));
			break;
		case R.id.fcs_control_layout:
			startActivity(new Intent(context.getApplicationContext(),ControlActivity.class));
			break;
		case R.id.fcs_time_layout:
			startActivity(new Intent(context.getApplicationContext(),SupervisonsActivity.class));
			
//			new Thread(new Runnable() {
//				public void run() {
//					Looper.prepare();
//
//					//和tcp服务器握手
//					ZKSocket.getInstance().handshake(MyApplication.getInstance().getId(), MyApplication.getInstance().getSessionToken()+"aaaa");
//				}
//			}).start();
//			
//	
			break;
		case R.id.fcs_special_number_layout:
			startActivity(new Intent(context.getApplicationContext(),SpecialNumberActivity.class));
			break;
		case R.id.fcs_qr_layout:
			startActivity(new Intent(context.getApplicationContext(),MyQrActivity.class));
			break;
		case R.id.fcs_patriarch_layout:
			startActivity(new Intent(context.getApplicationContext(),ParentsActivity.class));
			break;
		case R.id.fcs_binding_layout:
			startActivity(new Intent(context.getApplicationContext(),BindActivity.class));
			DroidWallHandle.getInstance(MyApplication.getInstance().getApplicationContext()).diableNetLockForced();
			break;
		case R.id.fcs_set_layout:  
			startActivity(new Intent(context.getApplicationContext(),SettingActivity.class));
//			DroidWallHandle.getInstance(MyApplication.getInstance().getApplicationContext()).enableNetLock();
			break;
		default:
			break;
		}
	}


}
